using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Plugin.DriftBottle.Scene;
using MicroMsg.Resource.@string;
using MicroMsg.Storage;
using MicroMsg.UI.Util;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace MicroMsg.Manager
{
	public class BConversationMgr
	{
		private const string TAG = "BConversationMgr";
		private EventWatcher bConversationModifyWatcher;
		private EventWatcher bContactModifyWatcher;
		private int addMsgCount;
		public BConversationMgr()
		{
			this.init();
			ChatMsgMgr.registerCleanMsg(new Action(BConversationMgr.delAllBottleConvAndSelfConv));
		}
		private void init()
		{
			EventCenter.registerEventHandler(EventConst.ON_STORAGE_CHAT_MSG_ADD, new EventHandlerDelegate(this.onMsgAdd));
			EventCenter.registerEventHandler(EventConst.ON_STORAGE_CHAT_MSG_MODIFY, new EventHandlerDelegate(this.onMsgModify));
			EventCenter.registerEventHandler(EventConst.ON_STORAGE_CHAT_MSG_DEL, new EventHandlerDelegate(this.onMsgDel));
			this.bConversationModifyWatcher = new EventWatcher(this, null, new EventHandlerDelegate(this.onBConversationChange));
			EventCenter.registerEventWatcher(EventConst.ON_STORAGE_BCONVERSATION_ADD, this.bConversationModifyWatcher);
			EventCenter.registerEventWatcher(EventConst.ON_STORAGE_BCONVERSATION_MODIFY, this.bConversationModifyWatcher);
			EventCenter.registerEventWatcher(EventConst.ON_STORAGE_BCONVERSATION_DEL, this.bConversationModifyWatcher);
			this.bContactModifyWatcher = new EventWatcher(this, null, new EventHandlerDelegate(this.onContactModify));
			EventCenter.registerEventWatcher(EventConst.ON_STORAGE_BOTTLECONTACT_ADD, this.bContactModifyWatcher);
			EventCenter.registerEventWatcher(EventConst.ON_STORAGE_BOTTLECONTACT_MODIFY, this.bContactModifyWatcher);
		}
		private void uinint()
		{
			EventCenter.removeEventHandler(EventConst.ON_STORAGE_CHAT_MSG_ADD, new EventHandlerDelegate(this.onMsgAdd));
			EventCenter.removeEventHandler(EventConst.ON_STORAGE_CHAT_MSG_MODIFY, new EventHandlerDelegate(this.onMsgModify));
			EventCenter.removeEventHandler(EventConst.ON_STORAGE_CHAT_MSG_DEL, new EventHandlerDelegate(this.onMsgDel));
			if (this.bConversationModifyWatcher != null)
			{
				EventCenter.removeEventWatcher(EventConst.ON_STORAGE_BCONVERSATION_ADD, this.bConversationModifyWatcher);
				EventCenter.removeEventWatcher(EventConst.ON_STORAGE_BCONVERSATION_MODIFY, this.bConversationModifyWatcher);
				this.bConversationModifyWatcher = null;
			}
			if (this.bContactModifyWatcher != null)
			{
				EventCenter.removeEventWatcher(EventConst.ON_STORAGE_BOTTLECONTACT_ADD, this.bContactModifyWatcher);
				EventCenter.removeEventWatcher(EventConst.ON_STORAGE_BOTTLECONTACT_MODIFY, this.bContactModifyWatcher);
				this.bContactModifyWatcher = null;
			}
		}
		private void onMsgAdd(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			List<ChatMsg> bMsgList = this.getBMsgList(evtArgs);
			if (bMsgList == null)
			{
				return;
			}
			List<AddMsgInfo> list = this.CombinationMsg(bMsgList);
			if (list != null && list.get_Count() != 0)
			{
				Log.smethod_5("BConversationMgr", "onChatAddMsgChange start");
				using (List<AddMsgInfo>.Enumerator enumerator = list.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						AddMsgInfo current = enumerator.get_Current();
						this.ChatAddMsg(current);
					}
				}
				Log.smethod_5("BConversationMgr", "onChatAddMsgChange end");
				return;
			}
		}
		private void onMsgModify(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			List<ChatMsg> bMsgList = this.getBMsgList(evtArgs);
			if (bMsgList == null)
			{
				return;
			}
			using (List<ChatMsg>.Enumerator enumerator = bMsgList.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					ChatMsg current = enumerator.get_Current();
					this.ChatUpdateMsg(current, false, 1);
				}
			}
		}
		private void onMsgDel(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			List<ChatMsg> bMsgList = this.getBMsgList(evtArgs);
			if (bMsgList == null)
			{
				return;
			}
			using (List<ChatMsg>.Enumerator enumerator = bMsgList.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					ChatMsg current = enumerator.get_Current();
					BConversation bConversation = StorageMgr.bconv.method_0(current.strTalker);
					if (bConversation != null && bConversation.nMsgLocalID == current.nMsgLocalID)
					{
						int msgNum = StorageMgr.chatMsg.getMsgNum(bConversation.strUsrName);
						if (msgNum <= 0)
						{
							bConversation.strContent = "";
							bConversation.nIsSend = 0;
							bConversation.nUnReadCount = 0;
							bConversation.strMsgType = 36.ToString();
							StorageMgr.bconv.modify(bConversation);
						}
						else
						{
							List<ChatMsg> msgList = StorageMgr.chatMsg.getMsgList(bConversation.strUsrName, msgNum - 1, 1);
							if (msgList != null && msgList.get_Count() > 0)
							{
								BConversationMgr.UpdateConversationFromMsg(msgList.get_Item(0), bConversation, false);
								StorageMgr.bconv.modify(bConversation);
							}
						}
					}
				}
			}
		}
		private void onBConversationChange(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			if (evtArgs.getListObject<BConversation>() == null)
			{
				return;
			}
			if (evtArgs.mEventID != EventConst.ON_STORAGE_BCONVERSATION_DEL)
			{
				EventCenter.postEvent(EventConst.ON_BCONVERSATION_LIST_CHANGE, evtArgs.mObject, null);
			}
			BConversationMgr.updateMainConversation(true);
		}
		private void onContactModify(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			List<BottleContact> listObject = evtArgs.getListObject<BottleContact>();
			if (listObject == null)
			{
				return;
			}
			for (int i = 0; i < listObject.get_Count(); i++)
			{
				this.UpdataBConversationFromBContact(listObject.get_Item(i));
				HeadImgMgr.getHeadImage(listObject.get_Item(i));
			}
		}
		private List<ChatMsg> getBMsgList(BaseEventArgs evtArgs)
		{
			List<ChatMsg> listObject = evtArgs.getListObject<ChatMsg>();
			if (listObject == null)
			{
				return null;
			}
			List<ChatMsg> list = null;
			using (List<ChatMsg>.Enumerator enumerator = listObject.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					ChatMsg current = enumerator.get_Current();
					if (ContactHelper.isBottleContact(current.strTalker))
					{
						if (list == null)
						{
							list = new List<ChatMsg>();
						}
						list.Add(current);
					}
				}
			}
			return list;
		}
		public ObservableCollection<BConversation> getList()
		{
			List<BConversation> list = StorageMgr.bconv.getList();
			if (list == null)
			{
				return null;
			}
			return new ObservableCollection<BConversation>(list);
		}
		private void UpdataBConversationFromBContact(BottleContact BContact)
		{
			if (BContact == null)
			{
				return;
			}
			List<BConversation> list = StorageMgr.bconv.getList();
			for (int i = 0; i < list.get_Count(); i++)
			{
				if (list.get_Item(i).strUsrName.Split(new char[]
				{
					':'
				})[0] == BContact.strUserName)
				{
					list.get_Item(i).strNickName = BContactMgr.getDisplayName(BContact.strUserName);
					StorageMgr.bconv.modify(list.get_Item(i));
					return;
				}
			}
		}
		private List<AddMsgInfo> CombinationMsg(List<ChatMsg> listArgs)
		{
			if (listArgs != null && listArgs.get_Count() != 0)
			{
				Log.smethod_5("BConversationMgr", "CombinationMsg,List count:" + listArgs.get_Count());
				List<AddMsgInfo> list = new List<AddMsgInfo>();
				Dictionary<string, int> dictionary = new Dictionary<string, int>();
				for (int i = 0; i < listArgs.get_Count(); i++)
				{
					ChatMsg chatMsg = listArgs.get_Item(i);
					if (chatMsg != null && !string.IsNullOrEmpty(chatMsg.strTalker))
					{
						if (dictionary.ContainsKey(chatMsg.strTalker))
						{
							int num = dictionary.get_Item(chatMsg.strTalker);
							if (chatMsg.nMsgLocalID > list.get_Item(num).chatMsg_0.nMsgLocalID)
							{
								list.get_Item(num).chatMsg_0 = chatMsg;
							}
							if (chatMsg.nIsSender == 1)
							{
								list.get_Item(num).msgCount = 0;
							}
							else
							{
								if (!chatMsg.IsSysInfo())
								{
									list.get_Item(num).msgCount++;
								}
								if (!chatMsg.IsSysInfo() && !PageManager.IsTalkingToUser(chatMsg.strTalker))
								{
									list.get_Item(num).msgCountWithoutTalker++;
								}
							}
						}
						else
						{
							dictionary.set_Item(chatMsg.strTalker, list.get_Count());
							AddMsgInfo addMsgInfo = new AddMsgInfo();
							addMsgInfo.chatMsg_0 = chatMsg;
							if (chatMsg.nIsSender == 1)
							{
								addMsgInfo.msgCount = 0;
							}
							else
							{
								if (!chatMsg.IsSysInfo())
								{
									if (!PageManager.IsTalkingToUser(chatMsg.strTalker))
									{
										addMsgInfo.msgCountWithoutTalker = 1;
									}
									addMsgInfo.msgCount = 1;
								}
								else
								{
									addMsgInfo.msgCount = 0;
								}
							}
							list.Add(addMsgInfo);
						}
					}
				}
				Log.smethod_5("BConversationMgr", "CombinationMsg,after combin List count:" + list.get_Count());
				return list;
			}
			return null;
		}
		private void ChatAddMsg(AddMsgInfo addInfo)
		{
			if (addInfo == null)
			{
				return;
			}
			if (addInfo.chatMsg_0 == null)
			{
				return;
			}
			if (!addInfo.chatMsg_0.IsSupport())
			{
				return;
			}
			if (addInfo.chatMsg_0.nIsSender != 1)
			{
				this.addMsgCount += addInfo.msgCount;
			}
			this.ChatUpdateMsg(addInfo.chatMsg_0, true, addInfo.msgCount);
		}
		private void ChatUpdateMsg(ChatMsg chatMsg_0, bool isAddMsg = false, int combinMsgCount = 1)
		{
			if (chatMsg_0 == null)
			{
				return;
			}
			if (!chatMsg_0.IsSupport())
			{
				return;
			}
			if (chatMsg_0.strTalker == "self@bottle:self")
			{
				return;
			}
			BConversation bConversation = StorageMgr.bconv.method_0(chatMsg_0.strTalker);
			if (bConversation == null)
			{
				bConversation = new BConversation();
				bConversation.nStatus = 1;
				BConversationMgr.UpdateConversationFromMsg(chatMsg_0, bConversation, isAddMsg);
				if (bConversation.nIsSend == 1)
				{
					bConversation.nUnReadCount = 0;
				}
				else
				{
					if (PageManager.IsTalkingToUser(bConversation.strUsrName))
					{
						bConversation.nUnReadCount = 0;
						this.SetMsgRead(bConversation, chatMsg_0);
					}
					else
					{
						bConversation.nUnReadCount = combinMsgCount;
						this.SetMsgUnRead(bConversation, chatMsg_0);
					}
				}
				Log.smethod_5("BConversationMgr", "StorageMgr.qconv.add start");
				StorageMgr.bconv.vmethod_0(bConversation);
				Log.smethod_5("BConversationMgr", "StorageMgr.qconv.add end");
				return;
			}
			if (!isAddMsg && bConversation.nMsgLocalID != chatMsg_0.nMsgLocalID)
			{
				return;
			}
			this.SetMsgRead(bConversation, chatMsg_0);
			BConversationMgr.UpdateConversationFromMsg(chatMsg_0, bConversation, isAddMsg);
			if (isAddMsg && chatMsg_0.nIsSender != 1)
			{
				if (PageManager.IsTalkingToUser(bConversation.strUsrName))
				{
					bConversation.nUnReadCount = 0;
					this.SetMsgRead(bConversation, chatMsg_0);
				}
				else
				{
					bConversation.nUnReadCount += combinMsgCount;
					this.SetMsgUnRead(bConversation, chatMsg_0);
				}
			}
			Log.smethod_5("BConversationMgr", "StorageMgr.qconv.update start");
			StorageMgr.bconv.update(bConversation);
			Log.smethod_5("BConversationMgr", "StorageMgr.qconv.update end");
		}
		public static void UpdateConversationFromMsg(ChatMsg chatMsg_0, BConversation conv, bool isNewMsg)
		{
			if (chatMsg_0 != null && conv != null)
			{
				conv.nMsgLocalID = chatMsg_0.nMsgLocalID;
				conv.strUsrName = chatMsg_0.strTalker;
				conv.nIsSend = chatMsg_0.nIsSender;
				if (isNewMsg)
				{
					conv.nTime = chatMsg_0.nCreateTime;
				}
				else
				{
					if (conv.nTime <= 0L)
					{
						conv.nTime = chatMsg_0.nCreateTime;
					}
				}
				if (chatMsg_0.IsVoice())
				{
					if (!chatMsg_0.IsSender() && ChatMsgMgr.getVoiceStatus(chatMsg_0) == MsgReadStatus.MsgUnRead)
					{
						conv.nStatus = 0;
					}
					else
					{
						conv.nStatus = 1;
					}
				}
				conv.strMsgType = string.Concat(chatMsg_0.nMsgType);
				conv.strNickName = ContactHelper.getDisplayName(chatMsg_0.strTalker, null);
				conv.strContent = chatMsg_0.strMsg;
				if (conv.strContent != null && conv.strContent.get_Length() >= 200)
				{
					conv.strContent = conv.strContent.Substring(0, 200);
				}
				return;
			}
		}
		public static void updateMainConversation(bool ifForceGenerateConv = true)
		{
			if (!BConversationMgr.IsShowBConvInMainPage())
			{
				return;
			}
			Conversation conversation = StorageMgr.converation.method_0("floatbottle");
			if (conversation == null)
			{
				conversation = new Conversation();
				conversation.strUsrName = "floatbottle";
			}
			List<BConversation> list = StorageMgr.bconv.getList();
			if (list != null && list.get_Count() != 0)
			{
				BConversation bConversation = new BConversation();
				for (int i = 0; i < list.get_Count(); i++)
				{
					if (list.get_Item(i).nTime > bConversation.nTime)
					{
						bConversation = list.get_Item(i);
					}
				}
				conversation.strContent = bConversation.strContent;
				conversation.strNickName = bConversation.strUsrName;
				conversation.strMsgType = bConversation.strMsgType;
				conversation.nIsSend = bConversation.nIsSend;
				conversation.nTime = bConversation.nTime;
				conversation.nStatus = bConversation.nStatus;
				conversation.nUnReadCount = BConversationMgr.getUnReadCountAll();
				StorageMgr.converation.update(conversation);
				return;
			}
			if (!ifForceGenerateConv)
			{
				return;
			}
			conversation.strContent = "";
			conversation.nUnReadCount = 0;
			conversation.nIsSend = 0;
			conversation.strMsgType = 1.ToString();
			StorageMgr.converation.update(conversation);
		}
		public static int getUnReadCountAll()
		{
			List<BConversation> list = StorageMgr.bconv.getList();
			if (list != null && list.get_Count() > 0)
			{
				int num = 0;
				using (List<BConversation>.Enumerator enumerator = list.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						BConversation current = enumerator.get_Current();
						num += current.nUnReadCount;
					}
				}
				return num;
			}
			return 0;
		}
		private void SetMsgRead(BConversation item, ChatMsg chatMsg_0)
		{
			if (chatMsg_0 == null)
			{
				return;
			}
			if (item == null)
			{
				return;
			}
			item.nStatus = 1;
			if (chatMsg_0.IsVoice() && chatMsg_0.nIsSender == 0 && !ChatMsgMgr.getVoiceMsgIsRead(chatMsg_0))
			{
				item.nStatus = 0;
			}
		}
		public void SetMsgUnRead(BConversation item, ChatMsg chatMsg_0)
		{
			if (chatMsg_0 == null)
			{
				return;
			}
			if (item == null)
			{
				return;
			}
			if (chatMsg_0.IsSysInfo())
			{
				item.nStatus = 1;
				return;
			}
			item.nStatus = 0;
		}
		public static string parseBMsgContentToShow(BConversation conv, ref string preUserNickName)
		{
			preUserNickName = null;
			if (conv == null)
			{
				return "";
			}
			ChatMsg chatMsg = new ChatMsg();
			chatMsg.strTalker = conv.strUsrName;
			chatMsg.strMsg = conv.strContent;
			chatMsg.nIsSender = conv.nIsSend;
			try
			{
				chatMsg.nMsgType = int.Parse(conv.strMsgType);
			}
			catch (Exception ex)
			{
				chatMsg.nMsgType = 1;
				Log.smethod_1("BConversationMgr", ex.get_Message());
			}
			return BConversationMgr.parseBMsgContentToShow(chatMsg, ref preUserNickName);
		}
		public static string parseBMsgContentToShow(ChatMsg chatMsg_0, ref string preUserNickName)
		{
			preUserNickName = null;
			if (chatMsg_0 == null)
			{
				return "";
			}
			if (string.IsNullOrEmpty(chatMsg_0.strTalker))
			{
				return "";
			}
			if (chatMsg_0.strMsg == null)
			{
				chatMsg_0.strMsg = "";
			}
			string text = "";
			preUserNickName = ContactHelper.getDisplayName(chatMsg_0.strTalker, null);
			int nMsgType = chatMsg_0.nMsgType;
			switch (nMsgType)
			{
			case 1:
				if (!chatMsg_0.IsSender())
				{
					text = chatMsg_0.strMsg;
				}
				else
				{
					text = strings.Float_Bottle_Me + ": " + chatMsg_0.strMsg;
				}
				break;
			case 2:
				break;
			case 3:
				text = strings.ChatImgContent;
				if (chatMsg_0.IsSender())
				{
					text = strings.Float_Bottle_Me + ": " + text;
				}
				break;
			default:
				if (nMsgType != 34)
				{
					if (nMsgType == 47)
					{
						text = strings.ChatCustomSmileyContent;
						if (chatMsg_0.IsSender())
						{
							text = strings.Float_Bottle_Me + ": " + text;
						}
					}
				}
				else
				{
					text = strings.ChatVoiceContent;
					if (chatMsg_0.IsSender())
					{
						text = strings.Float_Bottle_Me + ": " + text;
					}
				}
				break;
			}
			return text;
		}
		public static bool setUnreadCount(string userName, int nUnReadCount, bool forcetoReaded = false)
		{
			if (string.IsNullOrEmpty(userName))
			{
				return false;
			}
			BConversation bConversation = StorageMgr.bconv.method_0(userName);
			if (bConversation == null)
			{
				return false;
			}
			if (forcetoReaded)
			{
				bConversation.nStatus = 1;
			}
			bConversation.nUnReadCount = nUnReadCount;
			return StorageMgr.bconv.modify(bConversation);
		}
		public static void delBottleMsg(string talker)
		{
			if (string.IsNullOrEmpty(talker))
			{
				return;
			}
			ChatMsgMgr.deleteMsgByTalker(talker);
		}
		public static bool delBottle(string talker)
		{
			BConversationMgr.delBottleMsg(talker);
			StorageMgr.bconv.method_3(talker);
			return DriftBottleServiceCenter.DelBottle(talker);
		}
		public static void delAllBottleConvAndMsg()
		{
			List<BConversation> list = StorageMgr.bconv.getList();
			using (List<BConversation>.Enumerator enumerator = list.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					BConversation current = enumerator.get_Current();
					BConversationMgr.delBottleMsg(current.strUsrName);
				}
			}
		}
		public static void delAllBottleConvAndSelfConv()
		{
			BConversationMgr.delAllBottleConvAndMsg();
			ConversationMgr.delConversation("floatbottle");
		}
		public static void DriftBottleClearAll()
		{
			List<BConversation> list = StorageMgr.bconv.getList();
			using (List<BConversation>.Enumerator enumerator = list.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					BConversation current = enumerator.get_Current();
					BConversationMgr.delBottleMsg(current.strUsrName);
					DriftBottleServiceCenter.DelBottle(current.strUsrName);
				}
			}
			StorageMgr.bconv.clear(false);
			ConversationMgr.delConversation("floatbottle");
			DriftBottleServiceCenter.cleanBottlePluginFile();
		}
		public static bool IsShowBConvInMainPage()
		{
			return AccountMgr.ShowBottleInChat;
		}
		public static void ShowBConvInMainPage(bool ifForceGenerateConv = true)
		{
			BConversationMgr.updateMainConversation(ifForceGenerateConv);
		}
		public static void HideBConvInMainPage()
		{
			Conversation conversation = StorageMgr.converation.method_0("floatbottle");
			if (conversation == null)
			{
				return;
			}
			StorageMgr.converation.method_2(conversation);
		}
	}
}
